<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_pejabat extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.id_pejabat';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_pejabat, b.Nama AS NamaUniversitas, a.Nama AS NamaPejabat, c.nama_fakultas, d.nama_prodi, e.nama AS nama_jenis');
			$this->db->from('ref_pejabat a');
			$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.kd_fakultas','left');
			$this->db->join('ref_prodi d', 'd.kd_prodi = a.kd_prodi','left');
			$this->db->join('ref_jenisjabatan e', 'e.JenisJabatanID = a.JenisJabatanID');
			$this->db->where('a.status', "A");
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ref_pejabat a');
		$this->db->where('a.id_pejabat', $kode);
		
		return $this->db->get();
	}
	
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('kd_fakultas', $data['kd_fakultas']);
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('JenisJabatanID', $data['JenisJabatanID']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_pejabat');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('kd_fakultas', $data['kd_fakultas']);
		$this->db->set('kd_prodi', $data['kd_prodi']);
		$this->db->set('JenisJabatanID', $data['JenisJabatanID']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_pejabat', $data['id_pejabat']);
		$result = $this->db->update('ref_pejabat');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_pejabat', $kode);
		$result = $this->db->update('ref_pejabat');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}

	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('a.id_pejabat, b.Nama AS NamaUniversitas, a.Nama AS NamaPejabat, c.nama_fakultas, d.nama_prodi, e.nama AS nama_jabatan');
		$this->db->from('ref_pejabat a');
		$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.kd_fakultas','left');
		$this->db->join('ref_prodi d', 'd.kd_prodi = a.kd_prodi','left');
		$this->db->join('ref_jenisjabatan e', 'e.JenisJabatanID = a.JenisJabatanID');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Fakultas', 'width'=>50, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Program Studi', 'width'=>50, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Jabatan', 'width'=>65, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Pejabat', 'width'=>100, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nama_fakultas,
				$row->nama_prodi,
				$row->nama_jabatan,
				$row->NamaPejabat
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	function get_pdf(){
	
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('a.id_pejabat, a.Nama AS NamaPejabat, b.nama_fakultas, c.nama_prodi, d.nama AS nama_jabatan');
		$this->db->from('ref_pejabat a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.kd_fakultas','left');
		$this->db->join('ref_prodi c', 'c.kd_prodi = a.kd_prodi','left');
		$this->db->join('ref_jenisjabatan d', 'd.JenisJabatanID = a.JenisJabatanID');

		$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
	public function importData($data){
		//query insert data
		$this->db->flush_cache();
		
		$this->db->set('id_pejabat',				$data['id_pejabat']);
		$this->db->set('kd_pt',						$data['kd_pt']);
		$this->db->set('Nama',						$data['Nama']);
		$this->db->set('kd_fakultas',				$data['kd_fakultas']);
		$this->db->set('kd_prodi',					$data['kd_prodi']);
		$this->db->set('JenisJabatanID',			$data['JenisJabatanID']);
		$this->db->set('status',					$data['status']);
		
		$result = $this->db->insert('ref_pejabat');
		
		$errNo   = $this->db->_error_number();
	    $errMess = $this->db->_error_message();
		$error = $errMess;
		//var_dump($errMess);die;
	    log_message("error", "Problem import Inserting to : ".$errMess." (".$errNo.")"); 
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>